package mylibrary.thread.synchronize;

public class Operater implements Runnable{

	String name;
	BankSaving bs;
	
	public Operater(BankSaving bs, String name) {
		this.name = name;
		this.bs = bs;
	}
	
	//public static synchronized void operate(String name, BankSaving bs) {
	public static void operate(String name, BankSaving bs) {
		if (name.equals("husband")) {
			try {
				for (int i = 0; i < 10; i++) {
					Thread.currentThread().sleep((int)Math.random() * 3000);
					bs.add(1000);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				for (int i = 0; i < 10; i++) {
					Thread.currentThread().sleep((int)Math.random() * 3000);
					bs.get(1000);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void run() {
		operate(name, bs);
	}

}
